/*
 *  Copright: 2008 Philipp Leitner, Distributed Systems Group,
 *            TU Wien, leitner@infosys.tuwien.ac.at
 *
 *  This file is part of DAIOS.
 *
 *  DAIOS is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation.
 * 
 *  DAIOS is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with DAIOS.  If not, see
 *  <http://www.gnu.org/licenses/>.
 * 
 */

package at.ac.tuwien.infosys.dsg.daios.wsdl.impl;

import javax.xml.stream.XMLStreamException;

import org.apache.axiom.om.OMElement;

import at.ac.tuwien.infosys.dsg.daios.framework.interfaces.IConstants;
import at.ac.tuwien.infosys.dsg.daios.wsdl.interfaces.IWSDLMessage;
import at.ac.tuwien.infosys.dsg.daiosPlugins.axis2.axiom.OMUtil;

/**
 * A static helper class that implements a few often used
 * functions regarding WSDL.
 * 
 * @author philipp
 *
 */
public class WSDLUtil {
	
	/**
	 * Figure out the invcation syle (SOAP or REST) from
	 * a 'interface definition'.
	 * 
	 * <p>
	 * 
	 * The idea here is simple: if the 'interface' is a 
	 * WSDL file we assume that the invocation is a SOAP
	 * style invocation. If it is any other format (but
	 * still well-formed XML) we assume that it is an REST
	 * example.
	 * 
	 * @param document The 'interface definition' as String.
	 * @return The invocation style (SOAP or REST).
	 * @throws XMLStreamException
	 */
	public static String guessInvocationStyle(String document)
		throws XMLStreamException {
	
		// look at the starting tag of the document given.
		// If the URL points to a WSDL definition we assume
		// a WS call, if it is something else we assume REST
		
		OMElement root = OMUtil.getRootNode(document);
		
		// TODO: we have to change this for WSDL2
		boolean isWsdl = "definitions".equals(root.getLocalName());
	
		if(isWsdl)
			return IConstants.SOAP_STYLE;
		else
			return IConstants.REST_STYLE;
		
	}
	
	/**
	 * Check if this message is compliant to document/wrapped.
	 * 
	 * @param msgToUse The message to check.
	 * @return True if the message is compliant, false oterwise.
	 */
	public static boolean wrappedStyle(IWSDLMessage msgToUse) {
		
		// check if we have not more than one part...
		if(msgToUse.getParts().size() > 1)
			return false;
		else
			return true;
		
	}
	
}
